namespace BioService.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using System.Collections.Generic;
    using BioService.Database.Models;
    using BioService.Database.Context;

    internal sealed class Configuration : DbMigrationsConfiguration<BioService.Database.Models.Context>
    {

        private readonly BioDatabaseRepository repository = new BioDatabaseRepository();

        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(Context context)
        {
            AddUser(context);
        }

        private bool AddUser(Context context)
        {
            var users = new List<User> 
            {
                new User { Username = "John", Password = "OhBoyPlainText", PaypalInfo = new PaypalInfo { Email = "John123@gmail.com", Password = "OhBoyPlainText2" } },
                new User { Username = "Billy", Password = "AnotherPassword", PaypalInfo = new PaypalInfo { Email = "Billy123@gmail.com", Password = "AnotherPassword2" } },
                new User { Username = "Bob", Password = "Mooooo", PaypalInfo = new PaypalInfo { Email = "Bob123@gmail.com", Password = "Mooooo2" } },
                new User { Username = "Nope", Password = "Monkey", PaypalInfo = new PaypalInfo { Email = "Nope123@gmail.com", Password = "Monkey2" } },
                new User { Username = "Red", Password = "Blue", PaypalInfo = new PaypalInfo { Email = "Red123@gmail.com", Password = "Blue2" } }
            };

            users.ForEach(u => context.Users.Add(u));
            context.SaveChanges();

            return true;
        }

        private bool DeleteUser(Context context)
        {
            User user = context.Users.First(x => x.Username.Equals("Bob"));
            repository.Delete(user);
            user = context.Users.First(x => x.Username.Equals("Bob"));
            return (user == null);
        }

        private bool ReadUser(Context context)
        {
            User user = context.Users.First(x => x.Username.Equals("Nope"));
            Console.WriteLine("ID: " + user.UserID);
            Console.WriteLine("Username: " + user.Username);
            Console.WriteLine("Password: " + user.Password);
            Console.WriteLine("Paypal ID: " + user.PaypalInfo.PaypalInfoID);
            Console.WriteLine("Paypal Email: " + user.PaypalInfo.Email);
            Console.WriteLine("Paypal Password: " + user.Password);
            return true;
        }

        private bool UpdateUser(Context context)
        {
            User user = context.Users.First(x => x.Username.Equals("Red"));
            user.Username = "Purple";
            repository.Save(user);
            user = context.Users.First(x => x.Username.Equals("Purple"));
            return (user != null);
        }
    }
}
